package clientUtilGUI;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JToolBar;

import clientMain.Login;
import clientMain.Main;
import clientView.Administrator;
import clientView.User_Create;
import clientView.User_Edit;

public class Tab_Auth extends JToolBar implements ActionListener {
	private JButton btnUserCreate;
	private JButton btnUserEdit;
	private JButton btnAdmin;

	public Tab_Auth(){
		setFloatable(false);
		setOrientation(JToolBar.HORIZONTAL);
		setMargin(new Insets(0, 6, 0, 2));

		for(int i =0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("User") &&
					Login.listAuthorization.get(i).getPermName().equals("Add")){
			btnUserCreate = new JButton(UtilityGUI.subMenuUserCreate,new ImageIcon("src/image/add_user.png"));
			btnUserCreate.setVerticalTextPosition(JButton.BOTTOM);
			btnUserCreate.setHorizontalTextPosition(JButton.CENTER);
			btnUserCreate.addActionListener(this);
			add(btnUserCreate);
			}
		}
		
		for(int i =0; i<Login.listAuthorization.size(); i++){
			if(Login.listAuthorization.get(i).getModuleName().equals("User") &&
					Login.listAuthorization.get(i).getPermName().equals("Edit")){
				btnUserEdit = new JButton(UtilityGUI.subMenuUserEdit,new ImageIcon("src/image/edit-user-icon.png"));
				btnUserEdit.setVerticalTextPosition(JButton.BOTTOM);
				btnUserEdit.setHorizontalTextPosition(JButton.CENTER);
				btnUserEdit.addActionListener(this);
				add(btnUserEdit);
			}
		}
		
		if(Login.listAuthorization.get(0).getUserId() == 1){
			btnAdmin = new JButton(UtilityGUI.subMenuAdministrator,new ImageIcon("src/image/Administrator1.png"));
			btnAdmin.setVerticalTextPosition(JButton.BOTTOM);
			btnAdmin.setHorizontalTextPosition(JButton.CENTER);
			btnAdmin.addActionListener(this);
			add(btnAdmin);
		}
		
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnUserCreate){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabUserCreate);
			if(index == -1){
				Main.jTabbedPane.addTab(new User_Create(), UtilityGUI.tabUserCreate);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
		
		if(e.getSource() == btnUserEdit){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabUserEdit);
			if(index == -1){
				Main.jTabbedPane.addTab(new User_Edit(), UtilityGUI.tabUserEdit);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
		
		if(e.getSource() == btnAdmin){
			int index = Main.jTabbedPane.positionTab(UtilityGUI.tabAdministrator);
			if(index == -1){
				Main.jTabbedPane.addTab(new Administrator(), UtilityGUI.tabAdministrator);
				Main.jTabbedPane.setSelectedIndex(Main.jTabbedPane.getTabCount()-1);
			}else{
				Main.jTabbedPane.setSelectedIndex(index);
				return;
			}
		}
		
		
	}
}
